package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	UserAuthClient "gitee.com/liuxuezhan/ar-platform/rpc-client/userAuthClient/client/auth"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户实名认证-列表
func NewSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchListLogic {
	return &SearchListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SearchListLogic) SearchList(req *types.UserAuthSearchListRequest) (resp *types.UserAuthSearchListResponse, err error) {
	rpcResp, err := l.svcCtx.UserAuth.List(l.ctx, &UserAuthClient.ListReq{
		Pager: &UserAuthClient.Pager{
			Page:  int64(req.Page),
			Size:  int64(req.PageSize),
			Total: 0,
		},
		UserId:   req.UserId,
		RealName: req.Name,
		Idcard:   req.CPF,
	})
	if err != nil {
		return &types.UserAuthSearchListResponse{
			Pagination: &types.Pagination{},
			List:       []*types.UserAuthDetail{},
		}, err
	}
	list := make([]*types.UserAuthDetail, 0)
	for _, item := range rpcResp.Items {
		list = append(list, &types.UserAuthDetail{
			Id:       item.UserId,
			Name:     item.RealName,
			CPF:      item.Idcard,
			CreateAt: item.CreatedAt,
			UpdateAt: item.UpdatedAt,
		})
	}
	resp = &types.UserAuthSearchListResponse{
		Pagination: &types.Pagination{
			Page:     int32(rpcResp.Pager.Page),
			PageSize: int32(rpcResp.Pager.Size),
			Total:    rpcResp.Pager.Total,
		},
		List: list,
	}
	return
}
